<?PHP
session_start();
error_reporting(E_ALL);
// $Id: members.php
// written by Slyferz

//getting the user's access to see if they are allowed
include("includes/paxsql.php");
//$userid = $_SESSION['userid'];
//include("../recruitment/toolAccess.php");
//$access = getAcc('snowflake',$userid);
mysql_close();

include 'includes/database.php';
include 'includes/library.php';
//getting probation and afk options
$probate = addslashes($_GET['probate']);
$afk2 = addslashes($_GET['afk2']);
$regular = addslashes($_GET['nojob']);

function getKills($charID)
{
	$month = gmdate('m');
	$year = gmdate('Y');
	//$year = "20".date('y');
	$player[0][1] = $month;
	$number = 0;
	$chars = 0;
	//echo $charID." ";
	for($i = 0; $i < 6; $i++)
	{
		//making sure december processes correctly
		if($month == 12 && $i != 0)
		{
			$year--;
			$month1 = 1;
		}
		else if($month == 12 && $i == 0)
		{
			$check = 1;
		}
		else
		{
			$check = 0;
			$month1 = 0;
		}	
		//echo $year." ";
		//getting the span of the current month
		$time1 = gmmktime(0,0,0, $month, 1, $year);//$time1 += (9*60*60);
		//echo " ".$time1." ";
		if($month < 12 || $check == 1)$month++;
		else
		{
			$month -= 11;
			//$month = 1;
		}
		if($month1 == 1)
			$year++;
	//echo $year." ";	
		$time2 = gmmktime(0,0,0, $month, 0, $year);$time2 += (9*60*60);
		//echo $time2."|||";
		if($month1 == 1)
			$year--;
		//decreasing in months and adjusting the year
		//if it is next year
		if($month > 2) $month -= 2;
		else if($month == 2)
		{
		       	$month = 12;
			$year = date('Y');
		}
		else $month = 11;
		
		//getting the join information for the character
		$result = mysql_query("SELECT joined FROM characters WHERE realID = '".$charID."'") or die(mysql_error());
		$row = mysql_fetch_array($result);
		$joined = explode(' ',$row[0]);
		
		//getting number of kills grouping by pilot id
		$result = mysql_query("SELECT COUNT(*) as total FROM kills WHERE main_id = '".$charID."' AND timestamp >= '".$time1."' AND timestamp < '".$time2."'") or die(mysql_error());
		for($j = 0;$row = mysql_fetch_array($result); $j++) 
		{
			//checking for the timestamp of the player to make sure the kill is within
			//their join date
			//echo $time2." < ".$joined[0]." ";
			//echo $row["total"];
			if($time2 < ($joined[0] - (1*24*60*60))) $player[$i][0] = '*';
			else $player[$i][0] += $row["total"];
			//echo $row["total"];
		}
		
		$number += 3;
		if($player[$i][1] > 1) $player[($i+1)][1] = ($player[$i][1] - 1);
		else $player[($i+1)][1] = 12;
	}
	return $player;
}

function showMonth($charID, $regular)
{
	$result = mysql_query("SELECT afk FROM characters WHERE realID = '".$charID."' OR charID = '".$charID."'");
	$row = mysql_fetch_array($result);
	$afk = $row[0];
	$result = mysql_query("SELECT * FROM Jobs WHERE charID = '".$charID."'");
	$row = mysql_fetch_array($result);
	//choosing the color for the selected person according to status
	echo("<tr>");
	if($row['pos'] == 1) $background = 'fd78ff';
	else if($row['exempt'] == 1) $background = 'B5B3B2';
	else if($row['legacy'] == 1) $background = 'fcff00';
	else if($row['probation'] == 1) $background = 'ffa439';
	else if($afk == 1) $background = '52c8f2';
	else $background = 'fff';


	$player = getKills($charID);
	//displaying the months
	echo("<table style = 'margin:0 auto; color: #000; background: #".$background."' border = '1'>
		<tr><td>Months</td>");
	for($i = 6; $i > 0; $i--)echo("<td style='text-align:center;'>".date("F",gmmktime(0,0,0,$player[$i - 1][1]))."</td>");
	echo("</tr>");
	
	//displaying the kills
	echo("</tr><tr><td>Kills</td>");
	for($i = 6; $i > 0; $i--)
	{
		if(!strcmp($player[($i - 1)][0],'*')) echo("<td style='text-align:center;'>".$player[($i - 1)][0]."</td>");
		else if($player[($i - 1)][0] >= 5) echo("<td style='text-align:center;background: #85ff5f'>".$player[($i - 1)][0]."</td>");
		else if($player[($i - 1)][0] < 5) echo("<td style='text-align:center;background: #FE572A;'>".$player[($i - 1)][0]."</td>");
	}	
	echo "</tr>";
	echo("</table>");
}


function listMembers($probate, $afk2, $regular) {
	if($probate == 1)
	{
		$query = "SELECT distinct characters.realID as charID, characters.lastSeen, characters.inactive, characters.joined, characters.name
		FROM characters LEFT JOIN Jobs
		ON characters.realID = Jobs.charID
		WHERE Jobs.probation = 1
		AND characters.inCorp = 1
		ORDER BY characters.name";
	}
	else if($afk2 == 1)
	{
		$query = "SELECT characters.realID as charID, characters.name, characters.lastSeen, characters.inactive, characters.afk
			FROM characters
			WHERE characters.afk = 1
			AND characters.inCorp = 1
			ORDER BY characters.name";
	}
	else if($regular == 1)
	{
		$query = "SELECT distinct characters.realID as charID, characters.lastSeen, characters.inactive, characters.joined, characters.name
			FROM characters LEFT JOIN alts
			ON characters.realID = alts.charID
			WHERE alts.charID IS NULL
			AND characters.inCorp = 1
			AND characters.afk = 0
			AND characters.realID NOT IN(SELECT charID FROM Jobs)
			ORDER BY characters.name";
	}
	else
	{
		$query = "SELECT distinct characters.realID as charID, characters.lastSeen, characters.inactive, characters.joined, characters.name
			FROM characters LEFT JOIN alts
			ON characters.realID = alts.charID
			WHERE alts.charID IS NULL
			AND characters.inCorp = 1
			ORDER BY characters.name";
	}
	$results = mysql_query($query) or die(mysql_error());
	$numMain = mysql_num_rows($results);

	$month = date('m');
	$year = "20".date('y');
	$time = gmmktime(0,0,0, $month, 1, $year);
	$time += (9*60*60);
	//the killboard is unix time +9 hours

	while ($char = mysql_fetch_array($results)) 
	{
		echo("<td class = 'main'>");	
		echo("<a class = 'name' href = 'player.php?charID=".$char["charID"]."' style = 'font-weight:bold;'>".$char['name']."</a>");
		echo "</td>";
		echo("<td class='main'>".showMonth($char['charID'], $regular)."</td>");
		echo "</tr>";
	}
	echo "</table>";
}

if($access > 1)
{
echo("
<html>
<head>
<link rel='stylesheet' type='text/css' href='main.css'>
<title>snowflake</title>");

echo("
</head>

<body>

<div id='isAltWin' style='display: none;'></div>
<div id='flagWin' style='display: none;'></div>

<div id='wrapper'><!-- style = 'width: 750px;'-->

	<div id='header'><!--  style = 'width: 695px;'--><a href='/'>snowflake</a></div>

	<div id='navi'>");
	include('includes/navi.php');

	echo("
	<table width:150px;style='text-align:center;'>
		<tr><td style = 'text-align:center;'><br />Reference</td></tr>
		<tr><td style='background-color:#B5B3B2;' width = '20%'>Exempt/Officer</td></tr>
                <tr><td style='background-color:#52c8f2;' width = '20%'>AFK</td></tr>
                <tr><td style='background-color:#fd78ff;' width = '20%'>POS Duties</td></tr>
                <tr><td style='background-color:#fcff00;' width = '20%'>Legacy Members</td></tr>
		<tr><td style='background-color:#ffa439;' width = '20%'>Member on Probation</td></tr>
		<tr><td style = 'text-align:center;'><br />Kill Reference</td></tr>
		<tr><td style='background-color:#85ff5f;' width = '20%'>Meets Kill Requirement</td></tr>
		<tr><td style='background-color:#FE572A;' width = '20%'>Does Not Meet Kill Requirements</td></tr>
		<tr><td width = '20%'>* = Not in Corp</td></tr>
	</table>
	</div>

	<div id='main'><!--  style = 'width: 540px;'-->
		<h1>Kill Activity</h1>
		<hr>");
	//echo time()." ".(time()-(6*24*60*60))." ";
if($probate == 1)
{
	echo("<a class = 'name' href = '?'>Main</a> | 
		<a class = 'name' href = '?probate=1' style = 'font-weight:bold;'>Probation</a> | 
		<a class = 'name' href = '?afk2=1'>Afk</a> |
		<a class = 'name' href = '?nojob=1'>No Job</a>");
}
else if($afk2 == 1)
{
	echo("<a class = 'name' href = '?'>Main</a> | 
		<a class = 'name' href = '?probate=1'>Probation</a> | 
		<a class = 'name' href = '?afk2=1' style = 'font-weight:bold;'>Afk</a> |
		<a class = 'name' href = '?nojob=1'>No Job</a>");
}
else if($regular == 1)
{
	echo("<a class = 'name' href = '?'>Main</a> |
		<a class = 'name' href = '?probate=1'>Probation</a> | 
		<a class = 'name' href = '?afk2=1'>Afk</a> | 
		<a class = 'name' href = '?nojob=1' style = 'font-weight:bold;'>No Job</a>");
}
else
{
	echo("<a class = 'name' href = '?' style = 'font-weight:bold;'>Main</a> | 
		<a class = 'name' href = '?probate=1'>Probation</a> | 
		<a class = 'name' href = '?afk2=1'>Afk</a> | 
		<a class = 'name' href = '?nojob=1'>No Job</a>");
}
echo("<br /><br />");
listMembers($probate, $afk2, $regular);
	echo("</div>
	
</div>

</body>

</html>");
}
//they do not belong here, they get the boot
else
{
	header("Location: http://".$_SERVER['HTTP_HOST']."/index.php");
}
mysql_close();
?>
